<?php
	if (!isset($_GET['action'])) $action=NULL; else $action=$_GET['action'];
	if ($action==NULL) if (!isset($_POST['action'])) $action=NULL; else $action=$_POST['action'];
	if (!isset($_GET['product_id'])) $product_id=NULL; else $product_id=$_GET['product_id'];
	if ($product_id==NULL) if (!isset($_POST['product_id'])) $product_id=NULL; else $product_id=$_POST['product_id'];
	if (!isset($_GET['product_count'])) $product_count=NULL; else $product_count=$_GET['product_count'];
	if ($product_count==NULL) if (!isset($_POST['product_count'])) $product_count=NULL; else $product_count=$_POST['product_count'];
	
	if (!isset($_GET['source'])) $source=NULL; else $source=$_GET['source'];
	if ($source==NULL) if (!isset($_POST['source'])) $source=NULL; else $source=$_POST['source'];
	
	if (!isset($_GET['cart_id'])) $cart_id=NULL; else $cart_id=$_GET['cart_id'];
	if ($cart_id==NULL) if (!isset($_POST['cart_id'])) $cart_id=NULL; else $cart_id=$_POST['cart_id'];
	if ($cart_id==NULL) $cart_id = session_id();
	
	if ($act == 'clear') {	
		$cart_id = NULL; $card_group=NULL;
		if (!isset($_GET['cart_id'])) $cart_id=NULL; else $cart_id=$_GET['cart_id'];
		if ($cart_id==NULL) if (!isset($_POST['cart_id'])) $cart_id=NULL; else $cart_id=$_POST['cart_id'];
		if ($cart_id == NULL) $cart_id = session_id();
		if (!isset($_GET['card_group'])) $card_group=NULL; else $card_group=$_GET['card_group'];
		if ($card_group==NULL) if (!isset($_POST['card_group'])) $card_group=NULL; else $card_group=$_POST['card_group'];
		if ($card_group==NULL) return $shop_cartclass->ClearCart($cart_id);
		else return $shop_cartclass->ClearCart($cart_id, $card_group);
	} elseif ($act == 'remove_cart') {
		$cart_nr = NULL;
		if (!isset($_GET['cart_nr'])) $cart_nr=NULL; else $cart_nr=$_GET['cart_nr'];
		if ($cart_nr==NULL) if (!isset($_POST['cart_nr'])) $cart_nr=NULL; else $cart_nr=$_POST['cart_nr'];
		if (($_SESSION['client_login_id'] != 0) || ($_SESSION['client_login_id'] != "")) $user_id = $_SESSION['client_login_id'];
		else $user_id = 0;
		if ($cart_nr != NULL) $shop_cartclass->ClearCart($user_id, $cart_nr);
	} elseif ($act == 'remove') {	
		$shop_cartclass->RemoveCart($cart_id, $product_id);
	} elseif ($act == 'add') {
		$cart_nr = NULL;
		if (!isset($_GET['cart_nr'])) $cart_nr=NULL; else $cart_nr=$_GET['cart_nr'];
		if ($cart_nr==NULL) if (!isset($_POST['cart_nr'])) $cart_nr=NULL; else $cart_nr=$_POST['cart_nr'];
		if (isset($_POST['product_custom']) && is_array($_POST['product_custom'])) $product_custom=$_POST['product_custom']; else $product_custom=NULL;
		if (is_array($product_id) && is_array($product_count)){
			foreach($product_id as $p_id){
				if (!isset($product_custom[$p_id])) $product_custom[$p_id] = NULL;
				if ($cart_nr != NULL) $shop_cartclass->AddCart($cart_id, $p_id, $product_count[$p_id], $source, $cart_nr, $product_custom[$p_id]);
				else $shop_cartclass->AddCart($cart_id, $p_id, $product_count[$p_id], $source, '0', $product_custom[$p_id]);
			}
		} elseif ((intval($product_id)>0) && (intval($product_count)>0)){
			if (!isset($product_custom[$product_id])) $product_custom[$product_id] = NULL;
			if ($cart_nr != NULL) $shop_cartclass->AddCart($cart_id, $product_id, $product_count, $source, $cart_nr, $product_custom[$p_id]);
			else $shop_cartclass->AddCart($cart_id, $product_id, $product_count, $source, '0', $product_custom[$product_id]);
		}
	} elseif ($act == 'order_send') {	
		$cart_data = array(); $cart_info = array(); $mail_data = array(); $mail_settings = array();
		foreach($_POST as $key=>$el){
			if  ((strpos($key, "cart_info_") !== false) and (strpos($key, "cart_info_") == 0)) $cart_info[$key] = $el;
		}
		foreach($_POST as $key=>$el){
			if ((strpos($key, "form_data_") !== false) and (strpos($key, "form_data_") == 0)) $mail_data[$key] = $el;
			elseif ((strpos($key, "form_") !== false) and (strpos($key, "form_") == 0)) $mail_settings[$key] = $el;
		}
		
		$smarty->assign("mail_data", $mail_data);
		$smarty->template_dir = $setupdata['smarty_guest_template_dir'];
		if (isset($_POST['form_template'])){
			$tempalate = "guest/mail/".$_POST['form_template'];
			if ($smarty->template_exists($tempalate)) $output = $smarty->fetch($tempalate);
			else $output = "";
		} else $output = "";
		if (isset($_POST['form_template_text'])){
			$tempalate = "guest/mail/".$_POST['form_template_text'];
			if ($smarty->template_exists($tempalate)) $output_text = $smarty->fetch($tempalate);
			else $output_text = "";
		} else $output_text = "";
		if (isset($_POST['form_template'])){
			$smarty->clear_assign("mail_data");
			$treeclass->send_form_to_mail($output, $output_text, $mail_settings, $_FILES);
		}
		
		if (($_SESSION['client_login_id'] != 0) || ($_SESSION['client_login_id'] != "")) $user_id = $_SESSION['client_login_id'];
		else $user_id = 0;
	
		$cart_nr = NULL;
		if (!isset($_GET['cart_nr'])) $cart_nr=NULL; else $cart_nr=$_GET['cart_nr'];
		if ($cart_nr==NULL) if (!isset($_POST['cart_nr'])) $cart_nr=NULL; else $cart_nr=$_POST['cart_nr'];
	
		if ($cart_nr != NULL) $res = $shop_cartclass->OrderCart($cart_id, $mail_data, $cart_info, $_SESSION['lang'], $user_id, $cart_nr);
		else $res = $shop_cartclass->OrderCart($cart_id, $mail_data, $cart_info, $_SESSION['lang'], $user_id);
		if ($res != 0) $err->AddError($res, "shop_cart");
	}

?>
